package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;


@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;

    /**
     * 测试用
     * @param id
     * @return
     */
    @RequestMapping("/hello")
    @ResponseBody
    @RequireLogin
    public Object get(Long id) {
        return userInfoService.get(id);
    }

    /**
     * 验证手机号码是否存在
     * @param phone
     * @return
     */
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone) {
        //用户存在时,返回false,否则返回true
        return !userInfoService.checkPhone(phone);
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.success();
    }

    /**
     * 用户注册
     * 已做统一异常管理
     * @param phone
     * @param nickname
     * @param password
     * @param rpassword
     * @param verifyCode
     * @return
     */
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return new AjaxResult(true);
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @param response
     * @return
     */
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String username, String password, HttpServletResponse response) {
        String token = userInfoService.userLogin(username, password);

        CookieUtil.setCookie(token, response);
        return new AjaxResult(true);
    }

}
